package com.wshare.utility;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.wshare.entity.Account;
import com.wshare.entity.Role;

@Component
public class ElFunctions {

	public static String doMyStuff(String param) {
		return param;
	}

	@SuppressWarnings({ "unchecked", "resource" })
	public static List<Account> elGetAllAccounts() {
		String path = "classpath:hibernate-context.xml";

		FileSystemXmlApplicationContext app = new FileSystemXmlApplicationContext(
				path);
		SessionFactory sessionFactory = (SessionFactory) app
				.getBean("sessionFactory");
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Account.class);
		List<Account> accounts = criteria.list();

		return accounts;
	}
	
	@SuppressWarnings({ "resource", "unchecked" })
	public static List<Role> elGetRoleFromIdAccount(Integer idacc) {
		String path = "classpath:hibernate-context.xml";

		FileSystemXmlApplicationContext app = new FileSystemXmlApplicationContext(
				path);
		SessionFactory sessionFactory = (SessionFactory) app
				.getBean("sessionFactory");
		
		Session session = sessionFactory.openSession();
		String hql = "select r from Account acc inner join acc.lRoles r where acc.id =:idacc";
		Query query = session.createQuery(hql);
		query.setParameter("idacc",idacc);
		List<Role> lroles = query.list();
		return lroles;
	}
}
